package MULE.Model;

import java.util.Random;

import MULE.Model.Logic.MuleDriver;

/**
 * An enumeration of random events.
 * 
 * 
 * @ author Anastasiya Cotton @ version 1.0 11.06.2013
 */

public enum RandomEvent {

	GOOD_1("You received a mysterious package.", new Inventory(0, 3, 2, 0, 0),
			true), GOOD_2("You found a dead moose rat and sold its fur.",
			new Inventory(2, 0, 0, 0, 0), true), GOOD_3(
			"You got a bonus from the gods.", new Inventory(3, 0, 0, 0, 0),
			true), GOOD_4("Stranger left you some resources.", new Inventory(0,
			0, 2, 0, 0), true), GOOD_5("Town pays you for your heroic deeds.",
			new Inventory(1, 0, 0, 0, 0), true), BAD_1(
			"Your mule jumps off a cliff and you need to pay a hospital bill.",
			new Inventory(-6, 0, 0, 0, 0), false), BAD_2(
			"Your neighbor stole food from your kitchen!", new Inventory(0, -3,
					0, 0, 0), false), BAD_3(
			"UFO crashed into your house, the repairs cost you.",
			new Inventory(-5, 0, 0, 0, 0), false), BAD_4(
			"Your friend made a mess in the town, it cost you to clean it up.",
			new Inventory(-5, 0, 0, 0, 0), false), BAD_5(
			"Stranger broke your fence, and it costs you.", new Inventory(-2,
					0, 0, 0, 0), false);

	private String eventType;
	private Inventory consequence;
	private boolean isGood;

	/**
	 * Creates a new random event.
	 * 
	 * @param eventType
	 *            Message of the event.
	 * @param consequence
	 *            Inventory
	 * @param isGood
	 */
	private RandomEvent(String eventType, Inventory consequence, boolean isGood) {

		this.eventType = eventType;
		this.consequence = consequence;
		this.isGood = isGood;
	}

	/**
	 * Calculates the consequence of the random even based on the round.
	 * 
	 * @param roundNum
	 *            number of the round when random event is occurring
	 * @return inventory to be added or subtracted from the player (event
	 *         consequence)
	 */
	public Inventory getConsequence(int roundNum) {
		// Copy the base consequence.
		Inventory result = new Inventory();
		result.add(this.consequence);
		// Scale consequence.
		result.multiply(roundNum);
		return result;
	}

	/**
	 * Invokes a random event on the specified player.
	 * 
	 * @param p
	 *            The player for the random event to effect
	 * @param roundNumber
	 *            The round the event takes place in
	 * @param lowestScore
	 *            Whether the player has the lowest score
	 */
	public static void invokeRandomEvent(Player p, boolean hasLowestScore,
			int roundNumber) {
		Random rand = new Random();
		if ((rand.nextInt(100) + 1) < 27) {// 27% chance of an event happening
			RandomEvent something = null;
			if (hasLowestScore) {// if the player has the lowest score, only
									// pick from the bad ones
				something = RandomEvent.values()[rand.nextInt(5)];// random
																	// between 0
																	// and 4,
																	// the good
																	// ones
			} else {
				something = RandomEvent.values()[rand.nextInt(10)];// between 0
																	// and 9,
																	// all
																	// values
			}
			invokeEvent(something, p, roundNumber);
		}
	}

	/**
	 * Applies the consequences of the specified event to the given player.
	 * 
	 * @param e
	 *            The random event that should be invoked.
	 * @param p
	 *            The player who should take the consequences of the event.
	 * @param roundNumber
	 *            The round the event takes place in
	 */
	public static void invokeEvent(RandomEvent e, Player p, int roundNumber) {
		// Get consequences for this event.
		Inventory eventConsequence = e.getConsequence(roundNumber);
		// Display event text.
		String msg = p.getName() + ": " + e.eventType + "\n";
		msg += "You got " + eventConsequence;
		MuleDriver.showAlertDialog("Random Event", msg);
		// make inventory changes
		p.getInventory().add(eventConsequence);
	}
}
